package com.qianfeng.missfood.utils;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import com.qianfeng.missfood.BaseApp;
import com.qianfeng.missfood.constants.AppConfig;
import com.qianfeng.missfood.ui.LoginActivity;

import java.util.List;

/**
 * Created by Administrator on 15-8-17.
 */
public class FragmentTabUtils implements RadioGroup.OnCheckedChangeListener {

    private List<Fragment> fragments;
    private RadioGroup rgs;
    private FragmentManager fragmentManager;
    private int fragmentContentId;
    private int currentTab;
    private Activity activity;

    public FragmentTabUtils(Activity activity, FragmentManager fragmentManager, List<Fragment> fragments, int fragmentContentId, RadioGroup rgs) {
        this.activity = activity;
        this.fragments = fragments;
        this.rgs = rgs;
        this.fragmentManager = fragmentManager;
        this.fragmentContentId = fragmentContentId;
        // 默认显示第一页
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(fragmentContentId, fragments.get(0));
        ft.commit();
        rgs.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        int last = rgs.getChildCount();
        if (!BaseApp.getInstance().getLoginState()) {

            if (checkedId == rgs.getChildAt(rgs.getChildCount() - 1).getId()) {

                Intent intent = new Intent(activity, LoginActivity.class);
                activity.startActivityForResult(intent, AppConfig.ME_REQUEST);
                rgs.getChildAt(currentTab).performClick();
            }

            last = rgs.getChildCount() - 1;
        }

        for (int i = 0; i < last; i++) {
            if (rgs.getChildAt(i).getId() == checkedId) {
                Fragment fragment = fragments.get(i);
                FragmentTransaction ft = fragmentManager.beginTransaction();
//                getCurrentFragment().onPause(); // 暂停当前tab
                getCurrentFragment().onStop(); // 暂停当前tab
                if (fragment.isAdded()) {
                    fragment.onStart(); // 启动目标tab的fragment onStart()
//                    fragment.onResume(); // 启动目标tab的onResume()
                } else {
                    ft.add(fragmentContentId, fragment);
                    ft.commit();
                }
                showTab(i); // 显示目标tab
            }
        }
    }

    /**
     * 切换tab
     *
     * @param idx
     */
    private void showTab(int idx) {
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragments.get(i);
            FragmentTransaction ft = fragmentManager.beginTransaction();
            if (idx == i) {
                ft.show(fragment);
            } else {
                ft.hide(fragment);
            }
            ft.commit();
        }
        currentTab = idx; // 更新目标tab为当前tab
    }

    public Fragment getCurrentFragment() {
        return fragments.get(currentTab);
    }

}
